import {
    Api,
    ApiDataSource,
    ApiImport,
    console,
    Dao,
    Model,
    ModelOps,
    ModelPage,
    QueryParam,
    Service,
    Utils
} from 'yvan-server'
import {uploadService} from "../Support";

@Service
export default class GridService {
    @ApiDataSource
    selectAll(query: QueryParam): ModelPage<any[]> {
        const data = Dao.selectListAny("Grid.selectAll", query.toDaoParam());
        return ModelPage.newSuccess(null, query.pageDb, data);
    }

    @Api
    updateById(wheres: any, sets: any): ModelOps {
        const rz = Dao.updateAny("grid_datasource", sets, wheres);
        return ModelOps.newSuccess(rz);
    }

    @Api
    deleteById(deleteId: any): ModelOps {
        let wheres = {
            user_id: deleteId
        };
        const rz = Dao.deleteAny("grid_datasource", wheres);
        return ModelOps.newSuccess(rz);
    }

    @Api
    selectById(args: any): ModelOps {
        const data = Dao.selectListAny("Grid.selectById", {user_id: args.get("user_id")});
        if (data) {
            return ModelOps.newSuccess(data);
        }
        else {
            return ModelOps.newFail("操作失败");
        }
    }

    @Api
    addUser(args: any): ModelOps {

        const id = Utils.newId();
        const params = {
            user_id: id,
            login_name: args.get("login_name"),
            staff_name: args.get("staff_name"),
            gender: args.get("gender"),
            duty:args.get("duty"),
            mobile:args.get("mobile"),
            user_state:args.get("user_state"),
            login_count:args.get("login_count"),
            ui_style:args.get("ui_style"),
            birthday:args.get("birthday"),
        };
        let rz = Dao.save("Grid.addUser",params);
        if (rz == 1) {
            return ModelOps.newSuccess(id);
        }
        else {
            return ModelOps.newSuccess(rz);
        }
    }

    @ApiImport
    uploadImage(): ModelOps {
        const data = uploadService.uploadFile();
        return ModelOps.newSuccess(data);
    }

    @Api
    selectTest(args: any): ModelOps {
        const data = Dao.selectListAny("Grid.selectById", {user_id: args.get("user_id")});
        if (data) {
            return ModelOps.newSuccess(data);
        }
        else {
            return ModelOps.newFail("操作失败");
        }
    }
}